package 矩阵问题;


import java.util.Scanner;

public class 矩阵的阶乘 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[][] a = new long[N][N];
        long[][] b = new long[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                a[i][j] = b[i][j] = scanner.nextLong();
            }
        }

        if (M==0){
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (i==j){
                        System.out.print(1+" ");
                    }else {
                        System.out.print(0+" ");
                    }
                }
                System.out.println();
            }
        } else if (M==1){
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    System.out.print(a[i][j]+" ");
                }
                System.out.println();
            }
        }else {
            for (int m = 0; m < M-1; m++) {
                long[][] temp = new long[N][N];
                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < N; j++) {
                        long sum =0;
                        for (int y = 0; y < N; y++) {
                            sum = sum + a[i][y] * b[y][j];
                        }
                        temp[i][j] = sum;
                    }
                }
                b = temp;
            }
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    System.out.print(b[i][j]+" ");
                }
                System.out.println();
            }
        }

    }
}
